﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models.ViewPages;

namespace LeadVinaglue.Areas.Management.Controllers
{
    public class ContentListController : Controller
    {
        // Create an instance of user manage
        LeadVinaglue.Repository.ContentRepository repository = new LeadVinaglue.Repository.ContentRepository();

        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add()
        {
            ContentManagementModel model = new ContentManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator");

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add(FormCollection form)
        {
            ApplicationContent model = new ApplicationContent();

            model.Title = form["Title"].ToString();
            model.PageName = form["PageName"].ToString();
            model.Description = form["Description"].ToString();

            repository.Create(model);

            return RedirectToAction("Index", "ArchiveList");
        }

        // GET: /Management/ContentList/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            ContentManagementModel model = new ContentManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 0);

            return View(model);
        }

        //
        // GET: /Management/ContentList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id)
        {
            ContentManagementModel model = new ContentManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", id);
            ViewBag.ContentId = id;

            return View(model);
        }

        //
        // GET: /Management/ContentList/Edit/id
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id, FormCollection form)
        {
            ApplicationContent model = new ApplicationContent();

            if (model != null)
            {
                model.ID = id;
                model.PageName = form["PageName"].ToString();
                model.Title = form["Title"].ToString();
                model.Description = form["Description"].ToString();

                repository.Save(model);
            }

            return RedirectToAction("Index", "ContentList");
        }

        // Delete Email Item
        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Delete(int id)
        {
            repository.Delete(id);

            ContentManagementModel model = new ContentManagementModel();
            model = repository.GetDeleteModel(Helpers.Constant.HOME, "SuperAdministrator", id);

            return Json(model, JsonRequestBehavior.AllowGet);
        }
    }
}
